package com.bbs.service.impl;

import com.bbs.domain.Article;
import com.bbs.domain.Report;
import com.bbs.entity.Result;
import com.bbs.mapper.ArticleMapper;
import com.bbs.mapper.ReportMapper;
import com.bbs.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ReportServiceImpl
 * @Description TODO
 * @Author cy
 * @Date 2019/9/21 18:15
 * @Version 1.0
 **/

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询所有的被举报帖子
     * @return
     */
    @Override
    public List<Report> findAllReport() {

        return reportMapper.selectAll();
    }

    /**
     * 根据report表中的id查询article
     * @param report
     * @return
     */
    @Override
    public Article findOneArticle(Report report) {
        return articleMapper.selectByPrimaryKey(report);
    }

    /**
     * 屏蔽
     */
    @Override
    public void shield(Report report) {

        reportMapper.updateByPrimaryKeySelective(report);
    }

    /**
     * 驳回
     */
    public void reject(Report report) {

        reportMapper.updateByPrimaryKeySelective(report);
    }

    /**
     * 修改帖子状态
     * @param report
     */
    @Override
    public void updateReportStatus(Report report) {
        reportMapper.updateByPrimaryKeySelective(report);
    }


    /**
     * 举报某个帖子
     * @param reportContent
     * @param articleId
     */
    @Override
    public Result add(String reportContent, Integer articleId , String reportUserName) {
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if (article.getSendername()==reportUserName){
            return new Result(1,"不能举报自己的帖子");
        }
        Report report = new Report();
        report.setReportcontent(reportContent);
        report.setArticleid(articleId);
        report.setReporttime(new Date());
        report.setReportusername(reportUserName);
        report.setReportstatus(0);
        reportMapper.insert(report);
        return new Result(0,"举报成功");
    }
}
